<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree :data="list" default-expand-all>
          <!-- 使用el-tree组件提供作用域获取组件中数据 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="naogen" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<li class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item :success="hSuccess" @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length ===0" @click.native="hEle(data.id)">删除部门</el-dropdown-item>

                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree></el-card>

      <!-- 实现新增子部门或者编辑部门的对话框 -->
      <el-dialog
        :title="isEdit? '编辑部门':'新增子部门'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        @close="resetForm"
      >
        <!-- 组件 -->
        <addDialog

          :id="curId"
          ref="addOrEditRef"
          :is-edit="isEdit"
          :new-list="newList"
          @success="hSuccess"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
// 导入
import addDialog from '@/views/departments/deptDialog.vue'
import { getDepartments, deleteData } from '@/api/departments'
import { tranDataToTree } from '@/utils'
export default {
  components: {
    addDialog
  },
  data() {
    return {
      showDialog: false, // 控制新增子部门后编辑部门的对话框和显示和隐藏
      // 依赖一份树形数据
      list: [], // 部门列表
      curId: '', // 保存当前部门的id
      isEdit: false, // false新增子部门 | true 编辑部门
      newList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    resetForm() {
      this.$refs.addOrEditRef.resetForm()
    },
    // 关闭弹窗
    hSuccess() {
      // 重新渲染
      this.loadDepartments()
      // 关闭弹层
      this.showDialog = false
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      this.$nextTick(() => {
        this.$refs.addOrEditRef.loadDepartmentDetail()
      })
    },

    // 添加二级部门
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    // 删除
    hEle(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteData(id)
        // 重新渲染
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'

        })
      })
    },

    // 获取部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log(res, 'getDepartments')
        // res.data.depts 后端返回的部门列表是平铺 ---->需要转化为树形结构数据 形成
        // 删除数组的首个数据 shift是删除()
        console.log('res', res.data.depts)
        this.newList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })
        // console.log('newList', new_List)
        res.data.depts.shift()
        this.list = tranDataToTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>
